package com.joe.blockview.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.joe.blockview.R;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

public class BlockRightView extends BlockView {
    public BlockRightView(Context context) {
        super(context);
    }

    public BlockRightView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public BlockRightView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec,heightMeasureSpec);
        mBlockViewSize.x = selfWidth;
        mBlockViewSize.y = selfHeight;
        setMeasuredDimension(mBlockViewSize.x, mBlockViewSize.y);
    }


    private TextView tv;
    @Override
    public void init() {
        super.init();
        connection.setType(1);
        setStep("3");
        selfHeight = 150;
        selfWidth = 345;

        setCommand("Right");
        setName("Right");
        //添加输入控件
        tv = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.layout_tv_block, null, false);
        setcView(tv);
        ViewGroup.LayoutParams layoutParams = new LayoutParams(80,80);
        tv.setLayoutParams(layoutParams);
        tv.setText(this.getStep());
        tv.setTextColor(Color.rgb(239,197,67));
        addView(tv);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {

        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.rgb(239,197,67));

        //添加路径
        Path path1=new Path();
        path1.moveTo(10,0);
        path1.lineTo(40,0);
        path1.lineTo(60,20);
        path1.lineTo(80,20);
        path1.lineTo(100,0);
        path1.lineTo(250,0);
        path1.addArc(185,0,345,130,-90,180);
        path1.lineTo(250,130);
        path1.lineTo(100,130);
        path1.lineTo(80,150);
        path1.lineTo(60,150);
        path1.lineTo(40,130);
        path1.lineTo(10,130);
        path1.arcTo(0,110,20,130,90,90,false);
        path1.lineTo(0,30);
        path1.arcTo(0,0,20,20,180,90,false);

        //填充内部颜色
        canvas.drawPath(path1,paint);


        //绘制边界
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.rgb(195,148,31));
        paint.setStrokeWidth(2);
        canvas.drawPath(path1,paint);

//        paint.setColor(Color.WHITE);
//        paint.setTextSize(40);
//        paint.setTypeface(Typeface.DEFAULT_BOLD);
//        paint.setStyle(Paint.Style.FILL);
//        canvas.drawText("if",100,80,paint);

//        Path path = new Path();
//        path.moveTo(180,65);
//        path.lineTo(210,25);
//        path.lineTo(370,25);
//        path.lineTo(400,65);
//        path.lineTo(370,105);
//        path.lineTo(210,105);
//        path.lineTo(180,65);
//        paint.setColor(Color.rgb(60,92,169));
//        canvas.drawPath(path,paint);


        //添加图标
        Drawable d = getResources().getDrawable(R.drawable.block_right_ic);
        Bitmap bp = drawableToBitmap(d);
        canvas.drawBitmap(bp, 150f, 45f, paint);

//        Drawable d2 = getResources().getDrawable(R.drawable.block_circle_ic);
//        Bitmap bp2 = drawableToBitmap2(d2);
//        canvas.drawBitmap(bp2, 230f, 30f, paint);
//
//
//        paint.setColor(Color.rgb(239,197,67));
//        paint.setTextSize(40);
//        paint.setTypeface(Typeface.DEFAULT_BOLD);
//        paint.setStyle(Paint.Style.FILL);
//        canvas.drawText("3",258,82f,paint);
    }

    //drawable转bitmap
    private Bitmap drawableToBitmap(Drawable drawable) {
        Bitmap.Config config = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = Bitmap.createBitmap(45, 45, config);// 建立对应bitmap
        Canvas canvas = new Canvas(bitmap);// 建立对应bitmap的画布
        drawable.setBounds(0, 0, 45, 45);
        drawable.draw(canvas);// 把drawable内容画到画布中
        return bitmap;
    }

    //drawable转bitmap
    private Bitmap drawableToBitmap2(Drawable drawable) {
        Bitmap.Config config = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = Bitmap.createBitmap(80, 80, config);// 建立对应bitmap
        Canvas canvas = new Canvas(bitmap);// 建立对应bitmap的画布
        drawable.setBounds(0, 0, 80, 80);
        drawable.draw(canvas);// 把drawable内容画到画布中
        return bitmap;
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();

        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            if (child.getVisibility() == GONE) {
                continue;
            }

            if (child instanceof TextView) {
                int left = 230;
                int top = 30;
                int right = 230 + child.getMeasuredWidth();
                int bottom = 30 + child.getMeasuredHeight();
                child.layout(left, top, right, bottom);
            }
        }
    }
}
